import apiClient from '@/api/apiClient';

import { Menu } from '#/entity';
import { BasicStatus, MenuType } from '#/enum';
import { OperationPkForm } from '#/form';

export enum PermissionApi {
  List = '/security/menus',
  Flatten = '/security/flatten_menus',
  Menu = '/security/menu',
}

export interface RegionForm {
  parentId: number;
  name: string;
  code: string;
}

export interface MenuForm {
  parentId: number;
  name: string;
  type: MenuType;
  route: string;
  status?: BasicStatus;
  operationPkForm?: OperationPkForm;
  operationArray?: string[];
  sort?: number;
  icon?: string;
  component?: string;
  hidden?: boolean;
}

const menuList = (data: any) => apiClient.get<Menu[]>({ url: PermissionApi.List, params: data });

const flattenMenuList = (data: any) =>
  apiClient.get<Menu[]>({ url: PermissionApi.Flatten, params: data });

const createMenu = (data: MenuForm) => apiClient.post({ url: PermissionApi.Menu, data });

const deleteMenu = (id: number) => apiClient.delete({ url: `${PermissionApi.Menu}/${id}` });

const updateMenu = (id: number, data: MenuForm) =>
  apiClient.put({
    url: `${PermissionApi.Menu}/${id}`,
    data,
  });

export default {
  menuList,
  createMenu,
  updateMenu,
  deleteMenu,
  flattenMenuList,
};
